﻿using System;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using QuickShot.Windows;
using QuickShot.Shortcut;

namespace QuickShot.Forms.Controls
{
    public sealed class HotkeyControl : TextBox
    {
        private Keys _hotkey;
        public Keys Hotkey
        {
            get
            {
                return _hotkey;
            }
            set
            {
                _hotkey = value;
                Draw();
            }
    
        }

        private Keys _modifier = Keys.None;
        public Keys Modifier
        {
            get
            {
                return _modifier;
            }
            set
            {
                _modifier = value;
                Draw();
            }
        }

        private Timer prtScreenTimer = null;

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            //return base.ProcessCmdKey(ref msg, keyData);
            return false;
        }

        public HotkeyControl()
        {
            Cursor = Cursors.Arrow;
            Text = "None";
            prtScreenTimer = new Timer() 
            {
                Interval = 50
            };
            prtScreenTimer.Tick += PrtScreenTimer_Tick;
            GotFocus += ((s, e) =>
                {
                    WinAPI.HideCaret(Handle);
                    prtScreenTimer.Start();
                });
            LostFocus += ((s1, e1) =>
                {
                    CheckHotkey();
                    prtScreenTimer.Stop();
                    Draw(); 
                });
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
            {
                Clear();
                return;
            }
            Modifier = e.Modifiers;
            Hotkey = e.KeyCode;
            Draw();
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            CheckHotkey();
        }

        private void CheckHotkey()
        {
            if (Hotkey == Keys.None || Hotkey == Keys.Delete || Hotkey == Keys.Escape)
            {
                Modifier = Keys.None;
                Text = "None";
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void PrtScreenTimer_Tick(object sender, EventArgs e)
        {
            const int VK_SNAPSHOT = 0x2C;
            bool prtScreenDown = (WinAPI.GetKeyState(VK_SNAPSHOT) & 0x8000) != 0;
            if (prtScreenDown == true)
                OnKeyDown(new KeyEventArgs(Keys.PrintScreen | Control.ModifierKeys));
        }

        private new void Clear()
        {
            Modifier = Keys.None;
            Hotkey = Keys.None;
            Text = "None";
        }

        private void Draw()
        {
            if (Hotkey == Keys.ControlKey || Hotkey == Keys.Menu || Hotkey == Keys.ShiftKey)
            {
                Hotkey = Keys.None;
            }
            if (Hotkey == Keys.LWin || Hotkey == Keys.RWin)
            {
                Hotkey = Keys.None;
            }
            if (Modifier != Keys.None)
            {
                string modifier = Modifier.ToString().Replace(", ", " + ");

                if (Hotkey == Keys.None)
                    Text = modifier;
                else
                   Text = modifier + " + " + Hotkey.ToString();

                return;
            }
            if (Hotkey != Keys.None)
            {
                Text = Hotkey.ToString();
            }
        }
    }
}